#!/usr/bin/env python
# encoding: utf-8
"""
printUniqueCombinations:
__version__ = "1.1"
TAKEN FROM http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/66465
Written by:  Gagan Saksena; Modified by:  Brant Faircloth

Proc GENMOD Macro written by Jim Peterson, GA Fish and Wildlife Cooperative Research Unit GAFWCRU.

This script *can* generate all possible combinations of parameters for a logistic
regression in SAS.  However, some of our parameter combinations don't make sense.  
Therefore, we remove them from the candidate model set.

Additionally, the script if for logit regression but can be changed (since it
uses genmod, to poisson, normal, etc. regression.)

Remainder of script created by Brant Faircloth on 2008-02-11.
Copyright (c) 2008 Brant Faircloth. All rights reserved.

Copies of the macro code can be downloaded from the GAFWCRU and split into 2 files.
Contact Brant Faircloth for additional instructions.

"""
import sys, copy, pdb

models = []

def printList(alist):
    #print alist
    if alist != []:
        models.append(copy.deepcopy(alist))

def printUniqueCombinations(alist, numb, blist=[]):
    #pdb.set_trace()
    if not numb: return printList(blist)
    for i in range(len(alist)):
        blist.append(alist[i])
        printUniqueCombinations(alist[i+1:], numb-1, blist)
        blist.pop()

if __name__ == '__main__':
    # the list of parameters that we will build models containing
	k = ['y1','y2','survival','age_bin','p_cond_t','kernel_50_t','kernel_95_t','m_incubated','osr','osr1','osr2','osr3']
    n=len(k)+1
    for i in range(n):
        printUniqueCombinations(k, i)
    # search models backwards to remove biologically stupid models:
    for model in range(len(models))[::-1]:
        if models[model] == ['y1','y2','survival','age_bin','p_cond_t','kernel_50_t','m_incubated','osr']:
            print "Global Model being removed and placed at position 1"
            models.pop(model)
        elif 'y1' in models[model] and 'y2' not in models[model]:
            models.pop(model)
        elif 'y2' in models[model] and 'y1' not in models[model]:
            models.pop(model)
        elif 'kernel_50_t' in models[model] and 'kernel_95_t' in models[model]:
            models.pop(model)
        elif 'osr' in models[model] and 'osr1' in models[model] and 'osr2' in models[model] and 'osr3' in models[model]:
            #pdb.set_trace()
            #print models[model]
            models.pop(model)
        elif 'osr' in models[model] and 'osr2' in models[model] and 'osr3' in models[model]:
            #pdb.set_trace()
            #print models[model]
            models.pop(model)
        elif 'osr' in models[model] and 'osr1' in models[model] and 'osr3' in models[model]:
            #pdb.set_trace()
            #print models[model]
            models.pop(model)
        elif 'osr' in models[model] and 'osr1' in models[model] and 'osr2' in models[model]:
            #pdb.set_trace()
            #print models[model]
            models.pop(model)
        elif 'osr' in models[model] and 'osr1' in models[model]:
            #pdb.set_trace()
            #print models[model]
            models.pop(model)
        elif 'osr' in models[model] and 'osr2' in models[model]:
            #pdb.set_trace()
            #print models[model]
            models.pop(model)
        elif 'osr' in models[model] and 'osr3' in models[model]:
            #pdb.set_trace()
            #print models[model]
            models.pop(model)
        elif 'osr1' in models[model] and 'osr2' in models[model] and 'osr3' in models[model]:
            #pdb.set_trace()
            #print models[model]
            models.pop(model)
        elif 'osr1' in models[model] and 'osr2' in models[model]:
            #pdb.set_trace()
            #print models[model]
            models.pop(model)
        elif 'osr1' in models[model] and 'osr3' in models[model]:
            #pdb.set_trace()
            #print models[model]
            models.pop(model)
        elif 'osr2' in models[model] and 'osr3' in models[model]:
            #pdb.set_trace()
            #print models[model]
            models.pop(model)
    output = open('sas_input.sas','w')
    topFile = open('top.txt','rU')
    top = topFile.readlines()
    topFile.close()
    for line in top:
        output.write(line)
    output.write('\n%Macro modave(datafile = ,\n')
    for mod_num in range(len(models)):
        output.write(('\t\t\tPRED%s = ,\n') % (mod_num + 1))
    output.write('\t\t\tclassvar = ,\n')
    output.write('\t\t\tyvar = ,\n')
    output.write(('\t\t\tnum_mods = %s);\n\n') % (len(models) + 1))
    middleFile = open('middle.txt','r')
    middle = middleFile.readlines()
    middleFile.close()
    for line in middle:
        output.write(line)
    output.write('%modave(datafile = WORK.INP,\n')
    output.write('\t\t\tyvar = epp,\n')
    # write the global model statically
	output.write('\t\t\tPRED1 = y1 y2 survival age_bin p_cond_t kernel_50_t m_incubated osr,\n')
    # dynamically generate the other models
	for mod_num in range(len(models)):
        output.write(('\t\t\tPRED%s =') % (mod_num+2))
        for var in models[mod_num-1]:
            output.write((' %s') % (var))
        output.write(',\n')
    output.write(('\t\t\tclassvar = no,\n\t\t\tnum_mods = %s);\nrun;') % (len(models)+1))
    output.close()
    #pdb.set_trace()